package com.mew.chess.core.game.yxx.game.task;


import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.yxx.game.YxxGameManager;
import com.mew.chess.core.game.yxx.game.YxxRoomManager;
import com.mew.chess.core.game.yxx.game.common.YxxConstantValue;
import com.mew.chess.core.game.yxx.game.common.YxxLinkType;
import com.mew.chess.core.game.yxx.game.message.YxxPreparMessage;
import com.mew.chess.core.game.yxx.game.message.YxxPushCMDMapType;
import com.zebra.remoting.util.RemotingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class YxxPreparTask {

    public boolean startPrepar(int roomId){
        YxxRoomManager roomManager = YxxGameManager.getInstance().getController(roomId);

        roomManager.refreshBigRList();
        roomManager.clearPlayInfo();
        roomManager.setLinkType(YxxLinkType.Prepare.code());
        roomManager.setStartTime(System.currentTimeMillis());
        roomManager.setRuleTime(YxxConstantValue.PREPARE_TIME);

        sendStartMsg(roomManager);

        return true;
    }

    private void sendStartMsg(YxxRoomManager roomManager){
        YxxPreparMessage preparMessage = YxxPreparMessage.builder()
                .remainTime(YxxConstantValue.PREPARE_TIME)
                .build();
        roomManager.getOnlineUser().forEach((k, session) -> {
            if (null!=session && null!=session.getChannel()){
                ChessPushMessageUtil.pushMessage(session, YxxPushCMDMapType.PreparMessage.code(), preparMessage);
            }
        });
    }

}
